package controller.dialog;

import controller.KeyBordJogoEventos;
import java.util.Random;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * Dialog que  controlla o lançamento de dados
 * Implementa o padrão de projeto Singleton
 * @author Adriano
 */
public class DialogLancarDados {
    public static int um,dois;
    private static DialogLancarDados intancia;
    private static Stage s;
    private Parent root;
    
    public DialogLancarDados() {
        try{
            Screen screen = Screen.getPrimary();
            Rectangle2D bounds = screen.getVisualBounds();
            root = FXMLLoader.load(getClass().getResource("/view/Dados.fxml"));
            Scene scene = new Scene(root, Color.web("0xdfdfdfee"));
            scene.setOnKeyPressed(new KeyBordJogoEventos());
            
            //criar a cena e o palco do dialog
            s = new Stage(StageStyle.TRANSPARENT);
            s.setScene(scene);
            s.setX((bounds.getWidth()/2)-300);
            s.setY(bounds.getHeight()-190);
            s.initModality(Modality.APPLICATION_MODAL);
            
        }catch(Exception e){}
    }
    /**Retorna a único instância da classe*/
    public static DialogLancarDados getIntancia() {
        if (intancia == null) {
            intancia = new DialogLancarDados(); 
        }
        return intancia;
    }
    /**Mostrar o dialog*/
    public void show(){
        Random r = new Random();
        um = r.nextInt(6)+1;
        dois = r.nextInt(6)+1;
        
        ImageView dadoUm = (ImageView) root.lookup("#dadoUm");
        ImageView dadoDois= (ImageView) root.lookup("#dadoDois");
        Label somatorio = (Label) root.lookup("#somatorio");
        
        dadoUm.setImage(new Image("/resource/img/dado_"+um+".png"));
        dadoDois.setImage(new Image("/resource/img/dado_"+dois+".png"));
        somatorio.setText(""+(um+dois));
        s.show();
    }
    /**Fechar o dialog*/
    public void close(){
        s.close();
    }
}
